{% extends "base_generic.html" %}
{% block script %}
<script type="text/javascript">
  function generateUrl(name) {
    var sanitizedName = name;
    sanitizedName = sanitizedName.replace(/ el /gi, ' ');
    sanitizedName = sanitizedName.replace(/ la /gi, ' ');
    sanitizedName = sanitizedName.replace(/ los /gi, ' ');
    sanitizedName = sanitizedName.replace(/ las /gi, ' ');
    sanitizedName = sanitizedName.replace(/ de /gi, ' ');
    sanitizedName = sanitizedName.replace(/ del /gi, ' ');
    sanitizedName = sanitizedName.replace(/ temporada /gi, ' ');
    sanitizedName = string_to_slug(sanitizedName);
    sanitizedName = sanitizedName.replace(/--/gi, '-');
    sanitizedName = sanitizedName.replace(/---/gi, '-');
    sanitizedName = sanitizedName.replace(/----/gi, '-');
    $("#slug").val(sanitizedName);
  }
  
  function loadCategories() {
    var directory = $("#directory").val();
    $.getJSON('/admin/loadCategories/' + directory, function(data) {
     var categories = '<option></option>';
     for (i=0; i < data['categories'].length; i++){
       categories += ("<option value='" + data['categories'][i].categoryId + "'>" + data['categories'][i].name + "</option>");
     }
     $("#parent").html(categories);
    });
  }
  
  
  
  $(window).load(
    function() {
      $( "#firstPost" ).datepicker({ dateFormat: 'yy-mm-dd 00:00:00.000000' });
      
      var linkkey = "{{ category.key }}";
      
      if (linkkey == ""){
        loadCategories();
      }
      //loadCategories();
    });
</script>
{% endblock %}
{% block content %}
        <h1>Category</h1>
        <form action="/admin/categories/{{ category.key }}/save/" method="post">
        
        <div class="line">
          <div class="field">
            <label>Name:</label>
            <input id="name" name="name" type="text" size="45" value="{{ category.name }}" onblur="generateUrl(this.value);"  />
          </div>
          <div class="field">
            <label>Slug:</label>
            <input id="slug" name="slug" type="text" size="40" value="{{ category.slug }}" />
          </div>
        </div>
        <div class="line">
          <div class="field">
            <label>Directory:</label>
            <select id="directory" name="directory" onchange="loadCategories(); return false;">
              {% for directory in directories %}
              <option value="{{ directory.intname }}"{% ifequal category.directory directory.intname %} selected="selected"{% endifequal %}>{{ directory.name }}</option>
              {% endfor %}
            </select>
         </div>
          <div class="field">
            <label>Parent:{% if category.parentId %} <small style="text-transform: uppercase;">{{ category.parentId }} - {{ parent.name }}</small>{%endif%}</label>
            <select id="parent" name="parent">
              {% for parentcategory in categories %}
              <option value="{{ parentcategory.categoryId }}"{% ifequal parentcategory.categoryId category.parentId %} selected="selected"{% endifequal %}>{{ parentcategory.name }}</option>
              {% endfor %}
            </select>
         </div>
       </div>
         <div class="line">
           <div class="field">
             <label>Description:</label>
             <textarea cols="90" rows="4" id="description" name="description">{{ category.description }}</textarea>
           </div>
        </div>
        <input type="submit" value="Guardar" id="save" name="save" name="save" /><br class="clear" />
      </form>
      <br class="clear" />

{% endblock %}
      